package cn.com.xiaoshoujia.activities.activity;

import cn.com.xiaoshoujia.activities.dto.*;
import cn.com.xiaoshoujia.activities.mapper.ActivityMapper;
import cn.com.xiaoshoujia.activities.mapper.ActivityRulesMapper;
import cn.com.xiaoshoujia.activities.vo.ActivitySingleVO;
import cn.com.xiaoshoujia.order.mapper.ActivityOrderMapper;
import cn.com.xiaoshoujia.order.mapper.OrderMapper;
import cn.com.xiaoshoujia.utils.QueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dulei
 * @Title: ActivitiesOrderActivity
 * @Description: 订单Activity
 * @Time 2017/7/31 16:02
 */
@Repository
public class ActivitiesOrderActivity {

    Logger logger = LoggerFactory.getLogger(ActivitiesOrderActivity.class);

    @Autowired
    ActivityOrderMapper activityOrderMapper;

    @Autowired
    ActivityRulesMapper activityRulesMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ActivityMapper activityMapper;

    /**
     * @Author: dulei
     * @Description: 查询订单总信息
     * @param: id 活动ID type 类型 0团 1单买
     * @Date: 2017/8/1 8:55
     */
    public ActivityOrderSingleMsgDTO findOrderMsg(Long id, Integer type) {
        ActivityOrderSingleMsgDTO orderMsg = (ActivityOrderSingleMsgDTO) activityOrderMapper.findOrderMsg(id, type);
        if (null!=orderMsg){
            if (type.intValue()==0){
                BigDecimal headSuccess = activityOrderMapper.findHeadSuccess(id);
                orderMsg.setSettleAmount(headSuccess);
            }else {
                BigDecimal singleSuccess = activityOrderMapper.findSingleSuccess(id);
                orderMsg.setSettleAmount(singleSuccess);
            }
        }
        return orderMsg;
    }

    /**
     * @Author: dulei
     * @Description: 单买订单
     * @Date: 2017/8/2 8:58
     */
    public QueryResult listSingle(int pageNum, ActivitySingleVO activitySingleVO) throws UnsupportedEncodingException {
        QueryResult queryResult = new QueryResult();
        logger.debug("单买totalSize");
        Long totalSize = activityOrderMapper.findSingleCount(activitySingleVO);
        logger.debug("单买人信息集合");
        List<ActivityOrderDTO> list = activityOrderMapper.findSingleByPage(activitySingleVO);

        if (null != list)
            for (ActivityOrderDTO activityOrderDTO : list) {
                try {
                    activityOrderDTO.setUserName(URLDecoder.decode(activityOrderDTO.getUserName(), "utf-8"));
                    activityOrderDTO.setUserNickname(URLDecoder.decode(activityOrderDTO.getUserNickname(), "utf-8"));
                }catch (Exception e){
                    logger.error("解析用户昵称失败",e);
                    activityOrderDTO.setUserName("");
                    activityOrderDTO.setUserNickname("");
                }
            }
        Long totalPage = totalSize % activitySingleVO.getPageSize() == 0 ? totalSize / activitySingleVO.getPageSize() : totalSize / activitySingleVO.getPageSize() + 1;
        queryResult.setTotalPage(totalPage);
        queryResult.setTotalSize(totalSize);
        queryResult.setList(list);
        return queryResult;
    }

    /**
     * @Author: dulei
     * @Description: 拼团订单
     * @Date: 2017/8/2 8:58
     */
    public QueryResult listHead(int pageNum, ActivitySingleVO activitySingleVO) throws UnsupportedEncodingException {
        QueryResult queryResult = new QueryResult();
        BigDecimal bigDecimal = activityRulesMapper.findDiscount(activitySingleVO.getId());
        logger.debug("查询总条数");
        Long totalSize = activityOrderMapper.findHeadCount(activitySingleVO);
        logger.debug("团购订单");
        List<ActivityOrderHeadDTO> list = activityOrderMapper.findHeadByPage(activitySingleVO);

        if (null != list) {
            logger.debug("遍历赋值");
            for (ActivityOrderHeadDTO headOrder : list) {
                ActivityOrderHeadTotalDTO activityOrderHeadTotalDTO = (ActivityOrderHeadTotalDTO) activityOrderMapper.findHeadOrderTotal(headOrder.getHeadId());
                BeanUtils.copyProperties(activityOrderHeadTotalDTO, headOrder);
                try {
                    headOrder.setUserName(URLDecoder.decode(headOrder.getUserName(), "utf-8"));
                    headOrder.setUserNickname(URLDecoder.decode(headOrder.getUserNickname(), "utf-8"));
                }catch (Exception e){
                    logger.error("解析用户昵称失败",e);
                    headOrder.setUserName("");
                    headOrder.setUserNickname("");
                }
                headOrder.setDiscountPrice(bigDecimal);
            }
        }

        Long totalPage = totalSize % activitySingleVO.getPageSize() == 0 ? totalSize / activitySingleVO.getPageSize() : totalSize / activitySingleVO.getPageSize() + 1;
        queryResult.setTotalPage(totalPage);
        queryResult.setTotalSize(totalSize);
        queryResult.setList(list);
        return queryResult;
    }

    /**
     * @Author: dulei
     * @Description: 拼团活动绑定的活动信息
     * @param: id 拼团活动ID
     * @Date: 2017/8/2 8:58
     */
    public List<ActivityProductDTO> listProduct(Long id) {
        return activityOrderMapper.listProduct(id);
    }

    /**
     * @Author: dulei
     * @Description: 导出单买订单
     * @param: id 拼团活动ID
     * @Date: 2017/8/2 8:59
     */
    public List<Map<String, Object>> exportSingle(ActivitySingleVO activitySingleVO) throws UnsupportedEncodingException {

        List<Map<String, Object>> list = new ArrayList<>();
        logger.debug("获得下载集合");
        List<ActivitySingleExportDTO> exportList = activityOrderMapper.exportSingle(activitySingleVO);
        Map<String, Object> map = null;
        if (null != exportList)
            for (ActivitySingleExportDTO export : exportList) {
                map = new HashMap<>();
                map.put("userName", URLDecoder.decode(export.getUserName(), "utf-8"));
                map.put("cluesPhone", export.getCluesPhone());
                map.put("productName", export.getProductName());
                switch (export.getStatus()){
                    case "0":
                        map.put("status", "未支付");
                        map.put("refundAmount",new BigDecimal(0).toString());
                        map.put("SettleAmount","--");
                        break;
                    case "1":
                        map.put("status", "已成功" );
                        map.put("refundAmount",export.getRefundAmount().toString());
                        map.put("SettleAmount",export.getPrice().subtract(export.getRefundAmount()).toString());
                        break;
                    case "2":
                        map.put("status", "已失败");
                        map.put("refundAmount",new BigDecimal(0).toString());
                        map.put("SettleAmount",new BigDecimal(0).toString());
                        break;
                }
                map.put("orderId", export.getOrderId());
                map.put("ogPrice", export.getOgPrice());
                map.put("price", export.getPrice());
                list.add(map);
            }
        return list;
    }

    /**
     * @Author: dulei
     * @Description: 导出拼团订单
     * @Date: 2017/8/2 8:59
     */
    public List<Map<String, Object>> exportHead(ActivitySingleVO activitySingleVO) throws UnsupportedEncodingException {
        List<Map<String, Object>> list = new ArrayList<>();
        logger.debug("获得下载集合");
        List<ActivityOrderHeadDTO> exportList = activityOrderMapper.exportHead(activitySingleVO);
        BigDecimal bigDecimal = activityRulesMapper.findDiscount(activitySingleVO.getId());
        Map<String, Object> map = null;
        if (null != exportList)
            for (ActivityOrderHeadDTO export : exportList) {
                ActivityOrderHeadTotalDTO activityOrderHeadTotalDTO = (ActivityOrderHeadTotalDTO) activityOrderMapper.findHeadOrderTotal(export.getHeadId());
                map = new HashMap<>();
                map.put("userName", URLDecoder.decode(export.getUserName(), "utf-8"));
                map.put("cluesPhone", export.getCluesPhone());
                map.put("productName", export.getProductName());
                switch (export.getStatus().intValue()) {
                    case -1:
                        map.put("status", "已失败");
                        map.put("payAcount", activityOrderHeadTotalDTO.getPayAcount());
                        map.put("price", activityOrderHeadTotalDTO.getPrice());
                        map.put("refundAmount",activityOrderHeadTotalDTO.getRefundAmount().toString());
                        map.put("SettleAmount",activityOrderHeadTotalDTO.getPrice().subtract(activityOrderHeadTotalDTO.getRefundAmount()).toString());
                        break;
                    case 0:
                        map.put("status", "未支付");
                        map.put("payAcount", "--");
                        map.put("price","--");
                        map.put("refundAmount","--");
                        map.put("SettleAmount","--");
                        break;
                    case 1:
                        map.put("status", "进行中(未满)");
                        map.put("payAcount", activityOrderHeadTotalDTO.getPayAcount());
                        map.put("price", activityOrderHeadTotalDTO.getPrice());
                        map.put("refundAmount",activityOrderHeadTotalDTO.getRefundAmount().toString());
                        map.put("SettleAmount",activityOrderHeadTotalDTO.getPrice().subtract(activityOrderHeadTotalDTO.getRefundAmount()).toString());
                        break;
                    case 2:
                        map.put("status", "进行中(已满)");
                        map.put("payAcount", activityOrderHeadTotalDTO.getPayAcount());
                        map.put("price", activityOrderHeadTotalDTO.getPrice());
                        map.put("refundAmount",activityOrderHeadTotalDTO.getRefundAmount().toString());
                        map.put("SettleAmount",activityOrderHeadTotalDTO.getPrice().subtract(activityOrderHeadTotalDTO.getRefundAmount()).toString());
                        break;
                    case 3:
                        map.put("status", "已成功");
                        map.put("payAcount", activityOrderHeadTotalDTO.getPayAcount());
                        map.put("price", activityOrderHeadTotalDTO.getPrice());
                        map.put("refundAmount",activityOrderHeadTotalDTO.getRefundAmount().toString());
                        map.put("SettleAmount",activityOrderHeadTotalDTO.getPrice().subtract(activityOrderHeadTotalDTO.getRefundAmount()).toString());
                        break;
                    default:
                        map.put("status", "--");
                        map.put("payAcount","--");
                        map.put("price", "--");
                        map.put("refundAmount","--");
                        map.put("SettleAmount","--");
                        break;
                }
                map.put("acount", activityOrderHeadTotalDTO.getAcount());
                map.put("discountPrice", bigDecimal);
                list.add(map);
            }
        return list;
    }
}
